using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;

using Cards.Objects;
using Cards.Util;

namespace Cards.GUI
{
    public partial class INS_PackOfCards : UserControl
    {
        private int x;
        private int y;
        private int m_width;
        private int m_height;
        private Image m_picPath;
        private PictureBox[] m_pic;
        private CPackOfCards m_packsOfcard;
        private INS_Stage m_insStage;
        public ArrayList m_cardsArray = new ArrayList();

        private Label m_lMessage = new Label();
        private Timer m_hidingTimer;

        private Point m_pOCLoc;

        public CPackOfCards PC
        {
            get { return m_packsOfcard; }
            set { m_packsOfcard = value; }
        }
        public int WIDTH
        {
            get { return m_width; }
            set { m_width = value; }
        }
        public int HEIGHT
        {
            get { return m_height; }
            set { m_height = value; }
        }
        public int X
        {
            get { return x; }
            set { x = value; }
        }
        public int Y
        {
            get { return y; }
            set { y = value; }
        }
        public INS_Stage IS{
            get { return m_insStage; }
            set { m_insStage = value; }
        }
        public ArrayList CARDSARRAY
        {
            get { return m_cardsArray; }
            set { m_cardsArray = value; }
        }

        public Label MESSAGE
        {
            get { return m_lMessage; }
            set { m_lMessage = value; }
        }

        public Point POCLOC
        {
            get { return m_pOCLoc; }
            set { m_pOCLoc = value; }
        }

        public INS_PackOfCards(int _x, int _y, Image _picPath, INS_Stage _stage, int _width, int _height)
        {
            InitializeComponent();

            x = _x;
            y = _y;
            m_width = _width;
            m_height = _height;
            m_picPath = _picPath;
            m_packsOfcard = new CPackOfCards(this);
            m_insStage = _stage;
            Init();
        }

        public void Init()
        {
            this.Size = new Size(new Point(m_width, m_height));
            this.Location = new Point(x, y);

            m_packsOfcard.Init();

            m_lMessage.Visible = false;
            m_lMessage.Width = m_width - 20;
            m_lMessage.Height = m_height / 3;
            m_lMessage.BackColor = Color.Linen;
            m_lMessage.Location = new Point(10, m_height / 3);
            m_lMessage.BorderStyle = BorderStyle.FixedSingle;
            m_lMessage.TextAlign = ContentAlignment.MiddleCenter;
            m_lMessage.ForeColor = Color.Red;
            Font font = new Font(m_lMessage.Font, FontStyle.Bold);
            m_lMessage.Font = font;

            this.Controls.Add(m_lMessage);

            m_hidingTimer = new System.Windows.Forms.Timer();
            m_hidingTimer.Interval = 1500;
            m_hidingTimer.Tick += new System.EventHandler(hidingTimer_Tick);
        }

        public void CreateGUI()
        {
            m_pic = new PictureBox[Constants.PACK_OF_CARDS_HEIGHT];

            for (int i = 0; i < Constants.PACK_OF_CARDS_HEIGHT; i++)
            {
                m_pic[i] = new PictureBox();
                m_pic[i].Size = new Size(Constants.s_cardWidth, Constants.s_cardHeight);
                m_pic[i].Location = new Point(-i * Constants.PACK_OF_CARDS_SPACE + Constants.s_cardContainerWidth / 2 - Constants.s_cardWidth / 2,
                                               i * Constants.PACK_OF_CARDS_SPACE + Constants.s_cardContainerHeight / 2 - Constants.s_cardHeight / 2);
            }

            if (!Environment.CurrentDirectory.Equals(MainFrame.sCurDir))
            {
                Environment.CurrentDirectory = MainFrame.sCurDir;
            }

            for (int i = 0; i < Constants.PACK_OF_CARDS_HEIGHT; i++)
            {
                //Bitmap bmp = new Bitmap(m_picPath);
                //m_pic.Image = (Bitmap)bmp;
                m_pic[i].Image = m_picPath;
                m_pic[i].SizeMode = PictureBoxSizeMode.StretchImage;
                this.Controls.Add(m_pic[i]);
            }
            
            m_pOCLoc = new Point(Constants.s_cardContainerWidth / 2 - Constants.s_cardWidth / 2,
                                          Constants.s_cardContainerHeight / 2 - Constants.s_cardHeight / 2);
        }

        public void MergeCards()
        {
            m_packsOfcard.MergeCards();
        }

        public void DistributeVNPockerCards()
        {
            //m_packsOfcard.DistributeVNPockerCards(m_insStage[0].IC, m_insStage[1].IC, m_insStage[2].IC, m_insStage[3].IC);
            m_packsOfcard.DistributeVNPockerCards(m_insStage);
        }

        public void DistributeBlackJackCards()
        {
            m_packsOfcard.DistributeBlackJackCards(m_insStage);
        }

        public void HidePicture()
        {
            for (int i = 0; i < Constants.PACK_OF_CARDS_HEIGHT; i++)
            {
                m_pic[i].Visible = false;
            }
        }

        public void ShowPicture()
        {
            for (int i = 0; i < Constants.PACK_OF_CARDS_HEIGHT; i++)
            {
                m_pic[i].Visible = true;
            }
        }

        public void DisplayCards(int _pos)
        {
            for (int i = _pos; i < m_cardsArray.Count; i++)
            {
                Random rand = new Random();
                int irand = rand.Next(40);
                Console.Out.WriteLine(irand);
                ((INS_Card)m_cardsArray[i]).X = Constants.s_avatarWidth/2 
                                                + i*Constants.s_space_bw_two_cards + irand;
                ((INS_Card)m_cardsArray[i]).Y = irand + Constants.s_alphay;// +Constants.s_mobility;

                ((INS_Card)m_cardsArray[i]).CreateGUI();
                ((INS_Card)m_cardsArray[i]).MoveCard();

                this.Controls.Add(((INS_Card)m_cardsArray[i]));
                ((INS_Card)m_cardsArray[i]).BringToFront();
            }
        }

        public void RemoveCards()
        {
            for (int i = 0; i < m_cardsArray.Count; i++)
            {
                this.Controls.Remove(((INS_Card)m_cardsArray[i]));
                ((INS_Card)m_cardsArray[i]).Dispose();
            }
            m_cardsArray.Clear();
        }

        public void ShowMessage(string text)
        {
            m_lMessage.Visible = true;
            m_lMessage.Text = text;
            m_lMessage.BringToFront();

            m_hidingTimer.Start();
            m_hidingTimer.Enabled = true;
        }

        public void HideMessage()
        {
            m_lMessage.Visible = false;
            m_lMessage.Text = "";
        }

        protected void hidingTimer_Tick(object sender, EventArgs e)
        {
            m_hidingTimer.Stop();
            HideMessage();
        }

    }
}
